package com.learning.spring.ioc.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * ClassName: LifeCycleProcessor
 * Description: InstantiationAwareBeanPostProcessor 实现类
 * Date: 2018/8/3 13:24 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class LifeCycleBeanAwareProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    // 继承自 InstantiationAwareBeanPostProcessor 接口自身的，在 Bean 的实例化（Instantiation）前后

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        Object o = super.postProcessBeforeInstantiation(beanClass, beanName);
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation() 方法 ");
        return o;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation() 方法 ");
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        PropertyValues propertyValues = super.postProcessPropertyValues(pvs, pds, bean, beanName);
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessPropertyValues() 方法 ");
        return propertyValues;
    }


    // 继承自 BeanPostProcessor 接口，在 Bean 的初始化（Initialization）前后

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Object o = super.postProcessBeforeInitialization(bean, beanName);
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization() 方法 ");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Object o = super.postProcessAfterInitialization(bean, beanName);
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessAfterInitialization() 方法 ");
        return o;
    }
}
